package com.sebray.journal;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

public class JournalFile {

	private static final String DIR_PATH = "file:///E:/Data/Applications/Journal";
	private static final String FILE_PATH = DIR_PATH + "/Journal.txt";

	public JournalFile() {
		createFile();
	}

	private void createFile() {
		try {

			FileConnection dirc = (FileConnection) Connector.open(DIR_PATH);
			if (!dirc.exists()) {
				dirc.mkdir();
			}
			dirc.close();
			
			FileConnection fc = (FileConnection) Connector.open(FILE_PATH);
			if (!fc.exists()) {
				fc.create();
			}
			fc.close();
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("Error2");
		}
	}

	public void save(String text) {
		
		try {
			FileConnection connection = (FileConnection) Connector.open(
					FILE_PATH, Connector.WRITE);

	        OutputStream out = connection.openOutputStream(Long.MAX_VALUE);
	        PrintStream output = new PrintStream(out);
	        output.println(text);
	        output.println();
	        output.close();
	        out.close();
			connection.close();
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("Error1 : " + e.getMessage());
		}
	}
}
